#include "stdafx.h"
#include "ChunkManager.h"
#include "Chunk.h"
#include "Skybox.h"
#include "../StateMachine.h"
#include "../InteractManager.h"
#include "../../EdgeGraphics\Camera.h"
#include "../../EdgeGraphics/ConstantBuffer.h"
#include "../../EdgeGraphics/Lighting/LightingEngine.h"
#include "../../EdgeGraphics/BlendStateManager.h"
#include "../../EdgeContent/ContentManager.h"
#include "../../EdgeGraphics/GraphicsManager.h"
ChunkManager* ChunkManager::instance = nullptr;

ChunkManager::ChunkManager(void)
{
	/***********************Temporary********************************/
	// inilitize the interact manager
	InteractManager::GetInstance();
	chunks = new StateMachine;
	/***********************Temporary********************************/
	Chunk* chunk = new Chunk("Content/xml/betaLevel.lvl");

	chunks->setCurrentState(chunk);
	chunk->Release();
	/****************************************************************/

	skybox = new Skybox(ContentManager::GetInstance()->LoadTextureCube(L"Content/Textures/Skybox.dds"));
}

/*virtual*/ ChunkManager::~ChunkManager(void)
{
	delete chunks;
	delete skybox;
}

/*virtual*/ void ChunkManager::Update(float dt) /*override*/
{
	// update current chunk
	chunks->getCurrentState()->Update(dt);

	// TODO: if the player moves too close to the boundries
	// of the current chunk load/unload chunks

	// TODO: if the player gets close to the boundries of the 
	// currently generated world, generate new chunks
}

/*virtual*/ void ChunkManager::Draw(void) /*override*/
{
	LightingEngine* pLighting = LightingEngine::GetInstance();
	BlendStateManager* bsm = BlendStateManager::GetInstance();
	GraphicsManager* pGraphics = GraphicsManager::GetInstance();

	skybox->Draw();
	pGraphics->ClearDepthBuffer(1.f);
	pLighting->BeginDrawScene(Camera::GetInstance());
	
	// draw current chunk
	chunks->getCurrentState()->Draw();

	// TODO: Draw more than just the chunk the player is currently 
	// standing on

	pLighting->EndDrawScene();
	pLighting->RenderLights();

	bsm->SetBlendState(bsm->blendStates.alphaBlend);
	pLighting->DrawDiffered();
	bsm->SetBlendState(bsm->blendStates.opaque);
	
}

Chunk* ChunkManager::getActiveChunk(void) const
{
	return (Chunk*)chunks->getCurrentState();
}